package com.xxyf.controller;

import com.xxyf.service.LogUserDetaImp;
import com.xxyf.service.UsernameService;
import com.xxyf.tools.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author 小小怡飞
 * @Date 2022/9/1 5:27
 * @Version JDK 8
 */
@RestController
@RequestMapping("/login.do")
@Api(tags = "登录模块")
@Slf4j
public class Userlogin {
    @Autowired
    private UsernameService service;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @ApiModelProperty("注销用户")
    @GetMapping("/logout")
    public Rest logout(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication==null || authentication.getPrincipal()==null){
            return new Rest("注销成功");
        }
        LogUserDetaImp principal = (LogUserDetaImp)authentication.getPrincipal();
        redisTemplate.delete(principal.getUser().getId().toString());
        return new Rest("注销成功");
    }


    @ApiOperation("验证用户名登录")
    @PostMapping()
    private Rest lodging(@ApiParam("json传入account与password") @RequestBody Map<String,String> login){

        String user = login.get("user");
        String pass =  login.get("pass");
        return service.login(user,pass);

    }
    @ApiOperation("注册检测用户是否存在")
    @GetMapping("/check/{id}")
    private Rest check(@PathVariable("id") String id){
        return service.check(id);
    }
    @ApiOperation("注册用户")
    @GetMapping("/reg")
    private Rest reg(String account,String password,String newPass , String check){
        return service.reg(account,password,newPass,check);
    }
    @ApiOperation("获取验证码")
    @GetMapping("/code")
    private Rest code(String account){
        return service.code(account);
    }
}
